<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * Author: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */
namespace app\workflow\model;

/**
 * 频道字段模型
 */
class WorkflowDiyField extends WorkflowBase
{

    //扩展数据类型
    public function field_type_list($key = null)
    {
        $data = array(
            "varchar" => array('name' => '单行文本(varchar)', 'type' => 'varchar'),
            "textarea" => array('name' => '多行文本', 'type' => 'varchar'),
            "htmltext" => array('name' => 'HTML文本', 'type' => 'varchar'),
            "int" => array('name' => '整数类型', 'type' => 'int'),
            "float" => array('name' => '小数类型', 'type' => 'float'),
            "datetime" => array('name' => '时间类型(yyyy-mm-d H:i:s)', 'type' => 'datetime'),
            "date" => array('name' => '日期类型(yyyy-mm-d)', 'type' => 'date'),
            "imgurl" => array('name' => '图片(仅网址))', 'type' => 'varchar'),
            "img" => array('name' => '单图片', 'type' => 'varchar'),
            "imgs" => array('name' => '多个图片', 'type' => 'varchar'),
            "files" => array('name' => '文件附件', 'type' => 'varchar'),
            "option" => array('name' => '使用option下拉框', 'type' => 'varchar'),
            "radio" => array('name' => '使用radio选项卡', 'type' => 'varchar'),
            "checkbox" => array('name' => 'Checkbox多选框', 'type' => 'varchar'),
            "linkage" => array('name' => '系统内部关联', 'type' => 'varchar')
        );
        return ($key) ? $data[$key] : $data;
    }

    /**
     * 扩展字段表单显示
     * @param $ext_table //扩展表
     * @param $field_val_arr //是否有默认值
     * @return string
     * @author: 开发人生 goodkfrs@qq.com
     * @Time: 2023/4/9 14:48
     */
    public function getFieldExtHtml($ext_table, $field_val_arr = [])
    {

        $where['visible'] = ['=', '1'];
        $where['ext_table'] = ['=', $ext_table];
        $list = $this->modelWorkflowDiyField->getList($where, "", 'sort asc', false);
        $htmltxt = "";
        foreach ($list as $key => $row) {

            //判断字段是否存在字段值，主要针对更新表单时操作
            $field_value = array_key_exists($row["field_name"], $field_val_arr) ? $field_val_arr[$row["field_name"]] : "";
            $is_require_text = !empty($row['is_require']) ? '<span class="text-danger">(*)</span>' : '';

            switch ($row['field_type']) {
                case "varchar":
                    $htmltxt .= '
                            <div class="form-group">
                                <label class="col-sm-2 control-label">' . $row["show_name"] . $is_require_text . '</label>
                                <div class="col-sm-10">
                                    <input name="' . $row["field_name"] . '" class="form-control" type="text" value="' . $field_value . '"/>
                                    <span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
                                </div>
                            </div>';
                    break;
                case "textarea":
                    $htmltxt .= '
                            <div class="form-group">
                                    <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                    <div class="col-sm-10">
                                        <textarea name="' . $row["field_name"] . '" class="form-control" >' . $field_value . '</textarea>
                                        <span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
                                    </div>
                            </div>';
                    break;
                case "htmltext":
                    $htmltxt .= '<div class="form-group">
									        <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
									        <div class="col-sm-10">			
<textarea name="' . $row["field_name"] . '" class="form-control" >' . $field_value . '</textarea>
<span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
<link rel="stylesheet" href="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/editor/kindeditor/themes/default/default.css" />
<script src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/editor/kindeditor/kindeditor-all-min.js"></script>
<script src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/editor/kindeditor/lang/zh-CN.js"></script>
<script type="text/javascript">
    $(function(){
        var editor_' . $row["field_name"] . ';
        editor_' . $row["field_name"] . ' = KindEditor.create(\'textarea[name="' . $row["field_name"] . '"]\', {
                themesPath: KindEditor.basePath+\'/themes/\',//主题路径
                width: \'100%\',
                height: \'100px\',
                resizeType: 1,
                pasteType : 2,
                urlType : \'absolute\',
                fileManagerJson : \'\',
                uploadJson : \'{:addons_url("editor://Upload/pictureUpload")}\',
                items : [
                \'source\', \'undo\', \'redo\', \'cut\', \'copy\',\'paste\', \'plainpaste\', \'wordpaste\',\'selectall\',
                \'justifyleft\',\'justifycenter\',\'justifyright\',\'justifyfull\',\'insertorderedlist\',\'insertunorderedlist\',\'indent\',
                \'outdent\',\'subscript\',\'superscript\',\'fontname\',\'fontsize\',\'forecolor\',\'hilitecolor\',\'bold\',
                \'italic\',\'underline\',\'strikethrough\',\'removeformat\',\'image\',\'multiimage\',\'table\',
                \'link\',\'unlink\',\'fullscreen\'
                ],
                extraFileUploadParams: { session_id : \'{:session_id()}\'}
            });
        //ajax提交之前同步
        $(\'button[type="submit"],#submit,.ajax-post,#autoSave\').click(function(){
                editor_' . $row["field_name"] . '.sync();
        });
    });
</script>
									</div>
								</div>';
                    break;
                case "int":
                    $htmltxt .= '<div class="form-group">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">
                                                <input name="' . $row["field_name"] . '" class="form-control" type="text" value="' . $field_value . '"/>
                                                <span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
                                            </div>
                                        </div>';
                    break;
                case "float":
                    $htmltxt .= '<div class="form-group">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">
                                                <input name="' . $row["field_name"] . '" class="form-control" type="text" value="' . $field_value . '"/>
                                                <span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
                                            </div>
                                        </div>';
                    break;
                case "datetime":
                    $htmltxt .= '<div class="form-group">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">
                                                <input name="' . $row["field_name"] . '" class="form-control datetimepicker" type="text" value="' . $field_value . '"/>
                                                <span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
                                            </div>
                                        </div>';
                    break;
                case "date":
                    $htmltxt .= '<div class="form-group">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">
                                                <input name="' . $row["field_name"] . '" class="form-control datepicker" type="text" value="' . $field_value . '"/>
                                                <span class="help-block m-b-none">' . $row['field_desc'] . '</span> 
                                            </div>
                                        </div>';
                    break;
                case "option":
                    $htmltxt .= '<div class="form-group">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">
                                              <select data-placeholder="选择' . $row["show_name"] . '..." name="' . $row["field_name"] . '" class="chosen-select ' . $row["field_name"] . '-chosen-select">
                                        ';
                    $option_arr = str2arr($row['field_option'], '|');
                    foreach ($option_arr as $va) {
                        $option_chk = ($va == $field_value) ? "selected" : "";
                        $htmltxt .= '<option value="' . $va . '" hassubinfo="true" ' . $option_chk . '>' . $va . '</option>';
                    }
                    $htmltxt .= '
                                              </select>
                                            </div>
                                        </div>';
                    break;
                case "radio":
                    $htmltxt .= '<div class="form-group text-left">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">
                                                <div class="radio i-checks">
                                        ';
                    $option_arr = str2arr($row['field_option'], '|');
                    foreach ($option_arr as $va) {
                        $checked = ($va == $field_value) ? "checked" : "";
                        $htmltxt .= '<input type="radio" name="' . $row["field_name"] . '" value="' . $va . '" ' . $checked . ' /> ' . $va . ' ';
                    }
                    $htmltxt .= '
                                              </div>
                                            </div>
                                        </div>';
                    break;
                case "checkbox":
                    $htmltxt .= '<div class="form-group text-left">
									<label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
									<div class="col-sm-10">
										<div class="checkbox i-checks">
								';
                    $option_arr = str2arr($row['field_option'], '|');
                    $checked = ($va == $field_value) ? "checked" : "";
                    foreach ($option_arr as $va) {
                        $htmltxt .= '<input type="checkbox" name="' . $row["field_name"] . '" value="' . $va . '" ' . $checked . '/> ' . $va . ' ';
                    }
                    $htmltxt .= '
									  </div>
									</div>
								</div>';
                    break;
                case "linkage":
                    $htmltxt .= '
                                <div class="form-group">
									<label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
									<div class="col-sm-10">
									  <select data-placeholder="选择' . $row["show_name"] . '..." name="' . $row["field_name"] . '" class="chosen-select ' . $row["field_name"] . '-chosen-select" >
								';
                    $option_arr = $this->getExtFieldLinkage($row['field_option']);
                    foreach ($option_arr as $row) {
                        $htmltxt .= '<option value="' . $row['id'] . '" hassubinfo="true">' . $row['name'] . '</option>';
                    }
                    $htmltxt .= '
									  </select>
									</div>
								</div>';
                    break;
                case "img":
                    $pic_div = '';
                    if ($field_value) {
                        $pic_url = get_picture_url($field_value);
                        $pic_div .= '<div style="cursor:pointer; color:red;" class="pic_del"  onclick="picDel' . $row["field_name"] . '(this)" ><img src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/uploadify-cancel.png" /></div>';
                        $pic_div .= ' <a target="_blank" href="' . $pic_url . '"><img  style="max-width:150px;" src="' . $pic_url . '"/></a>';
                    }
                    $htmltxt .= '
                                        <div class="form-group text-left">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">';
                    $htmltxt .= '
                                        <link rel="stylesheet" href="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/Huploadify.css">
                                        <script src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/jquery.Huploadify.js"></script>
                                        <div id="upload_picture_' . $row["field_name"] . '"></div>
                                        <input type="hidden" name="' . $row["field_name"] . '" id="' . $row["field_name"] . '" value="0">
                                        <div class="upload-img-box' . $row["field_name"] . '"> ' . $pic_div . '</div>
                                        <script type="text/javascript">
                                            var maxwidth = "150px";
                                            $("#upload_picture_' . $row["field_name"] . '").Huploadify({
                                                auto: true,
                                                height: 30,
                                                fileObjName: "file",
                                                buttonText: "上传图片",
                                                uploader: "' . url('admin/File/pictureUpload', array('session_id' => session_id())) . '",
                                                width: 120,
                                                removeTimeout: 1,
                                                fileSizeLimit:"51200",
                                                fileTypeExts: "*.jpg; *.png; *.gif;",
                                                onUploadComplete: uploadPicturelitpic    });
                                                
                                                function uploadPicturelitpic(file, data)
                                                {
                                                    var data = $.parseJSON(data);
                                                    $("#' . $row["field_name"] . '").val(data.id);
                                                    var src =\'__STATIC__/upload/picture/\' + data.path;
                                                    var src =src.replace(/\/static/g, \'\');
                                                    $(".upload-img-box' . $row["field_name"] . '").html(\'<div class="upload-pre-item"> <a target="_blank" href="\' + src + \'"> <img style="max-width: \' + maxwidth + \';" src="\' + src + \'"/></a></div>\');
                                                }
                                        </script>                
                                        ';
                    $htmltxt .= '<span class="help-block m-b-none">' . $row['field_desc'] . '</span> ';
                    $htmltxt .= '</div></div>';
                    break;
                case "imgs":
                    $pic_div = '';
                    if ($field_value) {
                        $pic_arr = explode(',', $field_value);
                        foreach ($pic_arr as $value) {
                            $pic_url = get_picture_url($value);
                            $pic_div .= '
                                        <div class="upload-pre-item" style="float:left; margin: 10px;">
                                            <div style="cursor:pointer; color:red;" class="pic_del"  onclick="picDel' . $row["field_name"] . '(this, ' . $value . ')" >
                                                <img src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/uploadify-cancel.png" />
                                            </div>
                                            <a target="_blank" href="' . $pic_url . '"> <img style="width:150px;" src="' . $pic_url . '"/></a>
                                        </div>
                            ';
                        }
                    }
                    $htmltxt .= '
                                        <div class="form-group text-left">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">';
                    $htmltxt .= '
<link rel="stylesheet" href="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/Huploadify.css" />
<script src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/jquery.Huploadify.js"></script>
<div id="upload_pictures_' . $row["field_name"] . '"></div>
<input type="hidden" name="' . $row["field_name"] . '" id="' . $row["field_name"] . '" value="' . $field_value . '"/>
<div class="upload-img-box' . $row["field_name"] . '">' . $pic_div . '</div>
<script type="text/javascript">
    var maxwidth = "150px";
    $("#upload_pictures_' . $row["field_name"] . '").Huploadify({
        auto: true,
        height          : 30,
        fileObjName     : "file",
        buttonText      : "上传多个图片",
       // uploader        : "{:url(\'admin/File/pictureUpload\',array(\'session_id\'=>session_id()))}",
        uploader: "' . url('admin/File/pictureUpload', array('session_id' => session_id())) . '",
        width         : 120,
        removeTimeout	  : 1,
        fileSizeLimit:"51200",
        fileTypeExts: "*.jpg; *.png; *.gif;",
        onUploadComplete : uploadPicture' . $row["field_name"] . '
    });

    function uploadPicture' . $row["field_name"] . '(file, data){
        var data = $.parseJSON(data);
        var addons_name = "' . $row["field_name"] . '";
        var img_ids = $("#" + addons_name).val();
        var add_id = data.id;
        if(img_ids){ var lastChar = img_ids.charAt(img_ids.length - 1);  if(lastChar != \',\'){  add_id = img_ids + \',\' + add_id; } }
        $("#" + addons_name).val(add_id);
        //var src = \'/upload/picture/\' + data.path;
        var src =\'__STATIC__/upload/picture/\' + data.path;
        var src =src.replace(/\/static/g, \'\');
        $(".upload-img-box" + addons_name).append(\'<div class="upload-pre-item" style="float:left; margin: 10px;"> <div style="cursor:pointer; color:red;" class="pic_del"  onclick="picDel' . $row["field_name"] . '(this,\'+data.id+\')" ><img src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/uploadify-cancel.png" /></div> <a target="_blank" href="\' + src + \'"> <img style="max-width: \' + maxwidth + \';" src="\' + src + \'"/></a></div>\');
    }

    function picDel' . $row["field_name"] . '(obj, pic_id)
    {
        var addons_name = "' . $row["field_name"] . '";
        var img_ids = $("#" + addons_name).val();
        if(img_ids.indexOf(",") > 0)
        {
            img_ids.indexOf(pic_id) == 0 ? img_ids = img_ids.replace(pic_id + \',\', \'\') : img_ids = img_ids.replace(\',\' + pic_id, \'\');
            $("#" + addons_name).val(img_ids);
        }else{
            $("#" + addons_name).val(\'\');
        }
        $(obj).parent().remove();
    }
</script>
                                            ';

                    $htmltxt .= '<span class="help-block m-b-none">' . $row['field_desc'] . '</span> ';
                    $htmltxt .= '</div></div>';
                    break;

                case "files":
                    $pic_div = '';
                    if ($field_value) {
                        $pic_arr = explode(',', $field_value);
                        foreach ($pic_arr as $value) {
                            $pic_url = get_file_url($value);
                            $pic_div .= '
                                   <div class="upload-pre-file" style="float:left; margin: 10px;">
                                        <a target="_blank" href="' . $pic_url . '">' . $pic_url . '</a>
                                        <span style="cursor:pointer; color:red;" class="pic_del"  onclick="picDel' . $row["field_name"] . '(this, ' . $value . ')" >
                                            <img src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/uploadify-cancel.png" />
                                        </span>
                                    </div>
                            ';
                        }
                    }
                    $htmltxt .= '
                                        <div class="form-group text-left">
                                            <label class="col-sm-2 control-label">' . $row["show_name"] . '</label>
                                            <div class="col-sm-10">';
                    $htmltxt .= '
<link rel="stylesheet" href="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/Huploadify.css" />
<script src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/jquery.Huploadify.js"></script>
<div id="upload_pictures_' . $row["field_name"] . '"></div>
<input type="hidden" name="' . $row["field_name"] . '" id="' . $row["field_name"] . '" value="' . $field_value . '"/>
<div class="upload-img-box' . $row["field_name"] . ' clearfix">' . $pic_div . '</div>
<script type="text/javascript">
    var maxwidth = "150px";
    $("#upload_pictures_' . $row["field_name"] . '").Huploadify({
        auto: true,
        height          : 30,
        fileObjName     : "file",
        buttonText      : "上传文件",
       // uploader        : "{:url(\'admin/File/pictureUpload\',array(\'session_id\'=>session_id()))}",
        uploader: "' . url('admin/File/fileUpload', array('session_id' => session_id())) . '",
        width         : 120,
        removeTimeout: 1,
        fileSizeLimit:"51200",
        fileTypeExts: "*.doc; *.pdf; *.zip;*.txt;*.jpg; *.png; *.gif;",
        onUploadComplete : uploadPicture' . $row["field_name"] . '
    });

    function uploadPicture' . $row["field_name"] . '(file, data){
        var data = $.parseJSON(data);
        var addons_name = "' . $row["field_name"] . '";
        var img_ids = $("#" + addons_name).val();
        var add_id = data.id;
        if(img_ids){ var lastChar = img_ids.charAt(img_ids.length - 1);  if(lastChar != \',\'){  add_id = img_ids + \',\' + add_id; } }
        $("#" + addons_name).val(add_id);
        //var src = \'/upload/picture/\' + data.path;
        var src =\'__STATIC__/upload/file/\' + data.path;
        var src =src.replace(/\/static/g, \'\');
       // $(".upload-img-box" + addons_name).append(\'<div class="upload-pre-item" style="float:left; margin: 10px;"> <div style="cursor:pointer; color:red;" class="pic_del"  onclick="picDel' . $row["field_name"] . '(this,\'+data.id+\')" ><img src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/uploadify-cancel.png" /></div> <a target="_blank" href="\' + src + \'"> <img style="max-width: \' + maxwidth + \';" src="\' + src + \'"/></a></div>\');
        $(".upload-img-box" + addons_name).append(\'<div class="upload-pre-item" style="float:left; margin: 10px;"> <div style="cursor:pointer; color:red;" class="pic_del"  onclick="picDel' . $row["field_name"] . '(this,\'+data.id+\')" ><img src="' . STATIC_DOMAIN . SYS_DS_PROS . SYS_STATIC_DIR_NAME . '/addon/file/uploadify-cancel.png" /></div> <a target="_blank" href="\' + src + \'">\' + src + \'</a></div>\');
    }

    function picDel' . $row["field_name"] . '(obj, pic_id)
    {
        var addons_name = "' . $row["field_name"] . '";
        var img_ids = $("#" + addons_name).val();
        if(img_ids.indexOf(",") > 0)
        {
            img_ids.indexOf(pic_id) == 0 ? img_ids = img_ids.replace(pic_id + \',\', \'\') : img_ids = img_ids.replace(\',\' + pic_id, \'\');
            $("#" + addons_name).val(img_ids);
        }else{
            $("#" + addons_name).val(\'\');
        }
        $(obj).parent().remove();
    }
</script>
                                            ';

                    $htmltxt .= '<span class="help-block m-b-none">' . $row['field_desc'] . '</span> ';
                    $htmltxt .= '</div></div>';
                    break;

                default:
                    $htmltxt .= '';
            }
        }
        return $htmltxt;
    }

}
